package com.neusoft.ecommerce.controller;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.Provider;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.ProviderService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 87782
 */
@RestController
public class GoodsController {
    private final GoodsService goodsService;
    private final ProviderService providerService;

    public GoodsController(GoodsService goodsService, ProviderService providerService) {
        this.goodsService = goodsService;
        this.providerService = providerService;
    }

    @GetMapping("/goods_page")
    public ModelAndView goodsPage(){
        return new ModelAndView("goods/goods-list");
    }

    @RequestMapping("/goods/list")
    @ResponseBody
    public LayuiPager<Goods> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                  @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Goods> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = goodsService.findTotal(pager);
        List<Goods> list = goodsService.findList(pager);
        for (Goods goods : list) {
            System.out.println(goods);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }

    @GetMapping("/addGoods")
    public ModelAndView addGoods() {
        return new ModelAndView("goods/goods-add");
    }

    @RequestMapping("/goods/providerList")
    public List<Provider> findProviderList() {
        return providerService.findAll();
    }

    @PostMapping("/goods/add")
    public Map<String, Object> add(Goods goods) {
        System.out.println(goods);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsService.add(goods)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @GetMapping("/goods/edit_page")
    public ModelAndView editPage(Goods goods, Model model) {
        System.out.println(goods);
        LayuiPager<Goods> pager = new LayuiPager<>(1,1);
        pager.setCondition(goods);
        Goods message = goodsService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("goods/goods-edit");
        model.addAttribute("goods", message);
        return view;
    }


    @PostMapping("/goods/edit")
    public Map<String, Object> edit(Goods goods) {
        System.out.println(goods);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsService.edit(goods)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    @PostMapping("/goods/delete")
    public Map<String, Object> delete(Goods goods) {
        System.out.println(goods);
        Map<String, Object> map = new HashMap<>(16);
        if (goodsService.delete(goods)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
}
